package GUI.MarketingEmployee;

import javax.print.attribute.standard.DateTimeAtCompleted;
import javax.swing.JPanel;
import javax.swing.JButton;

import S2.server.ConnectionManage;
import analyzingSysetm.analyzingsystem;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.security.interfaces.RSAKey;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;

public class AnalyzeSystemPnl extends JPanel {

	/**
	 * Create the panel.
	 */
	private static Connection conn = ConnectionManage.getInstance()
			.getConnection();
	public JButton playSystemButton;
	public AnalyzeSystemPnl() {
		setLayout(null);
		
		playSystemButton = new JButton("Turn On");
		playSystemButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				try {
					String sql = "SELECT * from analyzingDate";
					ResultSet rs = null;
					try (PreparedStatement stmt = conn.prepareStatement(sql);) {
						rs = stmt.executeQuery();
					}
					Calendar now = Calendar.getInstance();
					now.add(Calendar.DATE, -7);
					int done=0;
					while(rs.next()){
						if((rs.getDate(1).getDate()==now.get(Calendar.DAY_OF_MONTH))
						&&(now.get(Calendar.MONTH)==rs.getDate(1).getMonth()+1)
						&&(now.get(Calendar.YEAR)==rs.getDate(1).getYear()))
						{
							done=1;
							analyzingsystem.updateClientImportance();
							analyzingsystem.updateInterestLevel();
						}
					}
					if(done==1)
					{
						sql = "DELETE * from analyzingDate";
						rs = null;
						try (PreparedStatement stmt = conn.prepareStatement(sql);) {
							rs = stmt.executeQuery();
						}
						sql = "INSERT INTO analyzingDate(date) values(?)";
						try (PreparedStatement stmt = conn.prepareStatement(sql);){
							stmt.setDate(1,(Date)Calendar.getInstance().getTime());
							rs = stmt.executeQuery();
						}
					}
					
				} catch (SQLException e) {
					e.printStackTrace();
				}
				
			}
		});
		playSystemButton.setForeground(Color.RED);
		playSystemButton.setBackground(Color.GREEN);
		playSystemButton.setBounds(169, 100, 143, 75);
		add(playSystemButton);
		

	}

}
